from zope.component import getUtility
from zope.component import getMultiAdapter
from plone.portlets.interfaces import IPortletManager
from plone.portlets.interfaces import IPortletAssignmentMapping

PORTLETS_TO_REMOVE = {u'plone.leftcolumn':[u'login',],
                      u'plone.rightcolumn':[ u'news', u'events', u'calendar',]}

PORTLETS_TO_ADD = {u'plone.leftcolumn':[(u'nobrasil','assignment'),],
                   u'plone.rightcolumn':[(u'listazope-pt','assignment'), \
                                         (u'pyconbrasil3','assignment'),]}

def adicionaPortletAssigments(portal):
    for p_manager in PORTLETS_TO_ADD.keys():
        manager = getUtility(IPortletManager, name=p_manager, context=portal)
        mapping = getMultiAdapter((portal, manager,), IPortletAssignmentMapping)
        for portlet,assignment in PORTLETS_TO_ADD[p_manager]:
            if portlet not in mapping:
                mapping[portlet] = assignment()
    
    leftColumn = getUtility(IPortletManager, name=u'plone.leftcolumn', context=portal)
    left = getMultiAdapter((portal, leftColumn,), IPortletAssignmentMapping, context=portal)
    if u'openid-login' not in left:
        print >>out, "Adding OpenID login portlet to the left column"
        left[u'openid-login'] = LoginAssignment()

def removePortletAssigments(portal):
    for p_manager in PORTLETS_TO_REMOVE.keys():
        manager = getUtility(IPortletManager, name=p_manager, context=portal)
        mapping = getMultiAdapter((portal, manager,), IPortletAssignmentMapping)
        for portlet in PORTLETS_TO_REMOVE[p_manager]:
            del mapping[portlet]    

def importVarious(context):
    if context.readDataFile('tchezope.politica_various.txt') is None:
        return
    
    portal = context.getSite()
    
    removePortletAssigments(portal)
